<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $product_id = $_POST['product_id'];
  $name = addslashes($_POST['name']);
  $width = addslashes($_POST['width']);
  $depth = addslashes($_POST['depth']);
  $height = addslashes($_POST['height']);
  $material = addslashes($_POST['material']);
  $detail = addslashes($_POST['detail']);
  
  $sql = "UPDATE products SET 
    product_title 	='$name',
    product_detail 	='$detail',
    product_width 	='$width',
    product_depth 	='$depth',
    product_height 	='$height',
    product_material 	='$material' 
    WHERE product_id=$product_id";

  $db->execute($sql);
  
  if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //$c_filename = $path ."/c_". $name;
    //
    //if (!move_uploaded_file($_FILES['img']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(278,188);
    //$image->save($filename);
    //
    //$image->load($c_filename);
    //$image->crop(274,121);
    //$image->save($c_filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    //
    //if(file_exists($path.'/'.$_POST['old_img']) && trim($_POST['old_img'])!=''){
    //  unlink($path.'/'.$_POST['old_img']);
    //}
    //
    //if(file_exists($path.'/c_'.$_POST['old_img'])){
    //  unlink($path.'/c_'.$_POST['old_img']);
    //}
    
    $handle = new Upload($_FILES['img']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 278;
      $handle->image_y = 188;
      $handle->Process('../../images/products/'.$product_id);
      $name = $handle->file_dst_name;
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img='$name' WHERE product_id=$product_id";
        if(mysql_query($sql)){
          if(file_exists('../../images/products/'.$product_id.'/'.$_POST['old_img']) && trim($_POST['old_img'])!=''){
            @unlink('../../images/products/'.$product_id.'/'.$_POST['old_img']);
          }
          
          if(file_exists('../../images/products/'.$product_id.'/c_'.$_POST['old_img'])){
            @unlink('../../images/products/'.$product_id.'/c_'.$_POST['old_img']);
          }
          
          $handle->file_name_body_pre = 'c_';
          $handle->image_resize = true;
          $handle->image_ratio_fill = true;
          $handle->image_x = 274;
          $handle->image_y = 121;
          $handle->Process('../../images/products/'.$product_id);
          $handle->processed;
        }else{
          @unlink('../../images/products/'.$product_id.'/'.$name);
          @unlink('../../images/products/'.$product_id.'/c_'.$handle->file_dst_name);
        }
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img1"]) && is_uploaded_file($_FILES["img1"]["tmp_name"]) && $_FILES["img1"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img1"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img1']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img1']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_2='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    //
    //if(file_exists($path.'/'.$_POST['old_img_2']) && trim($_POST['old_img_2'])!=''){
    //  unlink($path.'/'.$_POST['old_img_2']);
    //}
    
    $handle = new Upload($_FILES['img1']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      $name = $handle->file_dst_name;
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_2='$name' WHERE product_id=$product_id";
        if(mysql_query($sql)){
          if(file_exists('../../images/products/'.$product_id.'/'.$_POST['old_img_2']) && trim($_POST['old_img_2'])!=''){
            unlink('../../images/products/'.$product_id.'/'.$_POST['old_img_2']);
          }
        }else{
          unlink('../../images/products/'.$product_id.'/'.$name);
        }
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img2"]) && is_uploaded_file($_FILES["img2"]["tmp_name"]) && $_FILES["img2"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img2"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img2']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img2']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_3='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    //
    //if(file_exists($path.'/'.$_POST['old_img_3']) && trim($_POST['old_img_3'])!=''){
    //  unlink($path.'/'.$_POST['old_img_3']);
    //}
    
    $handle = new Upload($_FILES['img2']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      $name = $handle->file_dst_name;
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_3='$name' WHERE product_id=$product_id";
        if(mysql_query($sql)){
          if(file_exists('../../images/products/'.$product_id.'/'.$_POST['old_img_3']) && trim($_POST['old_img_3'])!=''){
            unlink('../../images/products/'.$product_id.'/'.$_POST['old_img_3']);
          }
        }else{
          unlink('../../images/products/'.$product_id.'/'.$name);
        }
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img3"]) && is_uploaded_file($_FILES["img3"]["tmp_name"]) && $_FILES["img3"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img3"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img3']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img3']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_4='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    //
    //if(file_exists($path.'/'.$_POST['old_img_4']) && trim($_POST['old_img_4'])!=''){
    //  unlink($path.'/'.$_POST['old_img_4']);
    //}
    
    $handle = new Upload($_FILES['img3']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      $name = $handle->file_dst_name;
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_4='$name' WHERE product_id=$product_id";
        if(mysql_query($sql)){
          if(file_exists('../../images/products/'.$product_id.'/'.$_POST['old_img_4']) && trim($_POST['old_img_4'])!=''){
            unlink('../../images/products/'.$product_id.'/'.$_POST['old_img_4']);
          }
        }else{
          unlink('../../images/products/'.$product_id.'/'.$name);
        }
      }
      $handle->clean();
    }
  }
  
  header("Location:product_list.php?aid=".$_POST['product_set_id']);
?>